//
//  BirthdayPickerView.m
//  project
//
//  Created by 圆圆陈 on 2018/1/20.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "BirthdayPickerView.h"

@interface BirthdayPickerView()
@property(nonatomic,strong)NSString *returnStr;
@end

@implementation BirthdayPickerView
{
    UIView *backView;
    UIDatePicker *birthdayPicker;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        _returnStr = [NSString string];
        backView = [[UIView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT*2/3, HHHWIDTH, HHHHEIGHT/3)];
        backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backView];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(FitW(10), FitH(10), FitW(70), FitH(30));
        [cancelBtn setTitleNormal:HHH_Text(@"退出")];
        [cancelBtn setTitleColorNormal:SNCOLORBLACK];
        [cancelBtn addTouchUpTarget:self action:@selector(cancelBtnClick)];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:cancelBtn];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame = CGRectMake(HHHWIDTH-FitW(70), FitH(10), FitW(50), FitH(30));
        [sureBtn setTitleNormal:HHH_Text(@"确定")];
        [sureBtn setTitleColorNormal:SNCOLORBLACK];
        [sureBtn addTouchUpTarget:self action:@selector(sureBtnClick)];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:sureBtn];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, FitH(50),HHHWIDTH , 1)];
        line.backgroundColor = SNCOLORGRAYLINE;
        [backView addSubview:line];
        
        birthdayPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, line.bottom+FitH(20), HHHWIDTH,backView.height-FitH(90))];
        birthdayPicker.datePickerMode = UIDatePickerModeDate;
        birthdayPicker.backgroundColor = [UIColor clearColor];
        birthdayPicker.tag = 100;
        birthdayPicker.maximumDate = [NSDate date];
        birthdayPicker.locale = [NSLocale currentLocale];
        [birthdayPicker setValue:SNCOLORBLACK forKey:@"textColor"]; //设置字体颜色
        [backView addSubview:birthdayPicker];
    }
    return self;
}
-(void)setBirthday:(NSString *)birthday{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"]; // 这里是用大写的 H
    NSDate *date = [formatter dateFromString:birthday];
    birthdayPicker.date = date;
}
-(void)cancelBtnClick{
    [self animationedDown];
}
-(void)sureBtnClick{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    _returnStr = [formatter stringFromDate:birthdayPicker.date];
    if (self.sureButtonBlock) {
        self.sureButtonBlock(_returnStr);
    }
    [self animationedDown];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:backView]) {
        return NO;
    }
    return YES;
}
@end
